package cn.sxt.oop;

import java.util.Arrays;

/**
 * Arrays数组测试
 */
public class ArraysTest {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 100, 20, 15};
        //打印数组
        System.out.println(Arrays.toString(a));

        //数组排序
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));

        //引用对象数组排序
        Man[] man = {new Man(3,3,"张三"), new Man(1,1,"赵一"), new Man(2,2,"杨二")};
        Arrays.sort(man);
        System.out.println(Arrays.toString(man));

        //二分法查找(使用二分法查找之前必须先排序)
        Arrays.sort(a);
        //二分法查找
        int i = Arrays.binarySearch(a, 100);
        System.out.println("查找的索引位置："+i);

        //数组替换
        //将索引[2,4)的元素替换为2333
        Arrays.fill(a,2,4,2333);
        System.out.println(Arrays.toString(a));
    }
}

//数组元素是引用类型的排序
class Man implements Comparable{
    private int age;
    private int id;
    private String name;
    public Man(int age, int id, String name){
        this.age = age;
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString(){
        return this.name;
    }

    //实现排序方法＜返回负数 ＞返回正数 ＝返回0
    @Override
    public int compareTo(Object o) {
        Man man = (Man)o;
        if(this.age < man.age){
            return -1;
        }
        if(this.age > man.age){
            return 1;
        }
        return 0;
    }
}
